





function addtowishlist(key) {
    let id =key.id;
    console.log('add to wish list , this product id:'+id);
}



let page_size= 8;


let commodity = {

    add_cart :function(item){
        let itemid = $(item).attr('id');

      $.ajax({
          type: 'POST',
          url: _common_util.getServerURL('cart/add_item'),
          dataType: 'json',
          data:{
              'itemId':itemid
          },
          success:function () {
              console.log('加入-'+itemid+'至购物车');
              window.wxc.xcConfirm('成功加入购物车','info');
          }
      })
    },


    getCommodity_list :function (keyword,categoryId,pageNum,pageSize,orderBy) {
        $.ajax({
            type: 'POST',
            url : _common_util.getServerURL('commodity/commodity_list'),
            dataType:'json',
            data: {
                'keyword': keyword,
                'categoryId':categoryId,
                'pageNum':pageNum,
                'pageSize':pageSize,
                'orderBy':orderBy
            },
            success:function (res) {
                $('#grid').find('.products').html('');
                $('#total-count').html(res.data.total);
                console.log(res);
                let count = 1;
                $.each(res.data.commodityVOS,function (i ,item) {
                     let detailurl ='single-product.html?itemId='+item.itemId;
                     let imgsrc = item.imageHost +item.image;

                     let first = ' <li class="product first">';
                     let last = ' <li class="product last">'
                     let strfirst = '';
                     if ( (count%4) === 1 ){
                         strfirst = strfirst +  first ;
                     }else if ((count%4) === 0){
                         strfirst = strfirst + last ;
                     }else {
                         strfirst = strfirst + ' <li class="product ">';
                     }
                     strfirst =  strfirst +
                        '                                            <div class="product-outer">\n' +
                        '                                                <div class="product-inner">\n' +
                        '                                                    <span class="loop-product-categories"><a href="'+detailurl+'" rel="tag"> '+item.categoryName+'</a></span>\n' +
                        '                                                    <a href="'+detailurl+'">\n' +
                        '                                                        <h3>  '+item.itemName+'</h3>\n' +
                        '                                                        <div class="product-thumbnail" style="overflow: hidden">\n' +
                        '\n' +
                        '                                                            <img data-echo="'+imgsrc+'" src="'+imgsrc+'" alt="" >\n' +
                        '\n' +
                        '                                                        </div>\n' +
                        '                                                    </a>\n' +
                        '\n' +
                        '                                                    <div class="price-add-to-cart">\n' +
                        '                                                        <span class="price">\n' +
                        '                                                            <span class="electro-price">\n' +
                        '                                                                <ins><span class="amount">&#036;  '+item.price+'</span></ins>\n' +
                        '                                                                <del><span class="amount">&#036;  '+item.price+'</span></del>\n' +
                        '                                                            </span>\n' +
                        '                                                        </span>\n' +
                        '                                                        <a rel="nofollow" onclick="commodity.add_cart(this)"  style="cursor: pointer" class="button add_to_cart_button" id="'+item.itemId+'">Add to cart</a>\n' +
                        '                                                    </div><!-- /.price-add-to-cart -->\n' +
                        '\n' +
                        '                                                    <div class="hover-area">\n' +
                        '                                                        <div class="action-buttons">\n' +
                        '                                                            <a onclick="addtowishlist(this)" rel="nofollow"  class="add_to_wishlist" id="'+ item.itemId+'">Wishlist</a>\n' +
                        '                                                            <a href="#" class="add-to-compare-link">Compare</a>\n' +
                        '                                                        </div>\n' +
                        '                                                    </div>\n' +
                        '                                                </div><!-- /.product-inner -->\n' +
                        '                                            </div><!-- /.product-outer -->\n' +
                        '                                        </li>';

                    $('#grid').find('.products').append(strfirst);
                    count ++;
                })

                totalpage = res.data.endRow;
                currentpage = res.data.pageNum;
                prepage = res.data.prePage;
                nextpage = res.data.nextPage;
                restpage = totalpage - currentpage;
                console.log("剩余的页数："+restpage);
                let pagestr='';
                $('#page-numbers').html('');

                if (currentpage>3){
                    //1和省略号
                     pagestr = pagestr+'               <li><a onclick="toPage(this)" page="1" class="page-numbers">1</a></li> \n' +
                        '                              <li class="apostrophe">...</li>' +
                         '                             <li><a onclick="toPage(this)" page="'+prepage+'" class="page-numbers">'+prepage+'</a></li>';
                }else {
                    for (var i = 1; i<currentpage;i++) {
                        pagestr = pagestr + '                <li><a onclick="toPage(this)" page="' + i + '" class="page-numbers">' + i + '</a></li>';
                    }
                }
                pagestr = pagestr + '                   <li><a onclick="toPage(this)" page="'+currentpage+'" class="page-numbers current">'+currentpage+'</a></li>\n';
                if (restpage>2){
                    pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li>       ' +
                                        '               <li class="apostrophe">...</li>\n' +
                                        '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                }else if (nextpage != totalpage && res.data.hasNextPage) {

                    pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li> ' +
                                        '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li> ';
                }else if (nextpage === totalpage){
                    pagestr = pagestr + ' <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                }
                $('#page-numbers').html(pagestr);

            },
            error : function (res) {
                console.log('error'+res)
            }
        })
    }

}

function toPage(item){
    let page =$(item).attr('page');
    let keyword =$('#input-search').val();
    console.log('cccccpage:'+page);
    let orderby =$('.orderby').val();
    let category = _common_util.getURLParam('category');
    commodity.getCommodity_list(keyword,category,page,page_size,orderby);
}

function select_pagenum(item){
    let selected_pagenum = $(item).val();
    page_size= selected_pagenum;
    let keyword =$('#input-search').val();
    let orderby =$('.orderby').val();
    let category = _common_util.getURLParam('category');

    console.log('select-pageNum:'+selected_pagenum);
    commodity.getCommodity_list(keyword,category,'',page_size,orderby);
}

function orderBy(item){
    let orderby = $(item).val();
    let keyword =_common_util.getURLParam('keyword');
    let category = _common_util.getURLParam('category');

    commodity.getCommodity_list(keyword,category,'',page_size,orderby);
}



$(function () {


    let selected_pagenum = $('#select-pagenum').val();
    page_size= selected_pagenum;
    let category = _common_util.getURLParam('category');
    if (category === null){
        let inputkeyword = _common_util.getURLParam('keyword');
        $('#input-search').val(inputkeyword);
        let keyword = $('#input-search').val();
        console.log('show=fw function');
        if (keyword != ''){
            $('#page-title').html('以下为  '+keyword+'  的搜索结果')
        }else {
            $('#page-title').html('');
        }
        page_size = 8 ;
        commodity.getCommodity_list(keyword,'','',page_size,'');

    }else {
        console.log(category);
            $('#page-title').html('以下为  '+category+'  的搜索结果')

        page_size = 8 ;
        commodity.getCommodity_list('',category,'','',page_size,'');
    }







    // $('#select-pagenum').on('change',function () {
    //     let selected_pagenum = $('#select-pagenum').val();
    //     page_size = selected_pagenum;
    //     let keyword =$('#input-search').val();
    //     console.log('select-pageNum:'+selected_pagenum);
    //     commodity.getCommodity_list(keyword,'',page,page_size,'');
    // })
})




